<template>
  <div class="page-container">
    <div class="control-container">
      <div class="left-panel shrink-panel" :class="isWide?'':'narrow'">
        <div class="shrink-btn-panel">
          <i :class="isWide?'el-icon-arrow-left':'el-icon-arrow-right'" @click="isWide = !isWide" />
        </div>
        <OutletTree
          v-if="isWide"
          @handleNodeClick="handleNodeClick"
        />
      </div>
      <div class="right-panel">
        <div class="search-panel">
          <el-form :inline="true" label-width="100">
            <el-form-item label="开始时间">
              <el-date-picker
                v-model="query.startTime"
                type="datetime"
                placeholder="请选择开始时间"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
            <el-form-item label="结束时间">
              <el-date-picker
                v-model="query.endTime"
                type="datetime"
                placeholder="请选择结束时间"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
            <el-form-item label="mn：">
              <el-select v-model="query.outletId" filterable clearable placeholder="请选择">
                <el-option
                  v-for="item in outletNames"
                  :key="'selectMn'+index+'_'+item.id"
                  :label="item.outletMn"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="排口名称：">
              <el-select v-model="query.outletId" filterable clearable placeholder="请选择">
                <el-option
                  v-for="item in outletNames"
                  :key="'selectName'+index+'_'+item.id"
                  :label="item.outletName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="报文类型：">
              <el-select v-model="query.dataType" placeholder="请选择" clearable>
                <el-option
                  v-for="item in dataTypes"
                  :key="item.code"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="解析状态：">
              <el-select v-model="query.packetStatus" placeholder="请选择" clearable>
                <el-option
                  v-for="item in packetStatuList"
                  :key="item.code"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
            <el-button type="primary" @click="handleSelect">查询</el-button>
            <el-button type="primary" @click="handleExport">导出</el-button>
          </el-form>
        </div>
        <div class="">
          <el-table
            ref="multipleTable"
            v-loading="tableLoading"
            class="grid-data-table"
            height="calc(100vh - 340px)"
            :data="tableData"
          >
            <el-table-column prop="outletName" label="排口名称" width="160" />
            <el-table-column prop="mn" label="mn" width="150" />
            <el-table-column prop="dataType" label="报文类型" width="120">
              <template slot-scope="scope">{{ getDictLabel('e038', scope.row.dataType) }}
              </template>
            </el-table-column>
            <el-table-column prop="packetStatus" label="解析状态" width="100">
              <template slot-scope="scope">{{ getDictLabel('e037', scope.row.packetStatus) }}
              </template>
            </el-table-column>
            <el-table-column prop="messageTime " label="报文时间" width="150">
              <template slot-scope="scope">{{ parseTime(scope.row.messageTime,'{y}-{m}-{d} {h}:{i}:{s}') }}
              </template>
            </el-table-column>
            <el-table-column prop="content" label="报文内容" />
            <el-table-column label="操作" width="100">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="handle(scope.row)">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <Pagination
              v-show="query.total>0"
              :total="query.total"
              :page.sync="query.page"
              :limit.sync="query.limit"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
    </div>
    <detailsDialog
      :dialog-visible="dialogVisible_d"
      :obj="obj"
      @closeDialog="closeDialog"
      @flushPage="handleSelect"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import detailsDialog from './components/detailsDialog'
import { exportPage, getPage } from '@/api/base/dataPacket'
import { getList } from '@/api/base/outlet'
import { downloadFileHasFile, getDictLabel, parseTime } from '@/utils'
import { getDict } from '@/utils/dict'
import OutletTree from '@/components/Tree/outletTree'
import { getDatefmt } from '@/utils/common'

export default {
  name: 'DataPacket',
  components: {
    Pagination, OutletTree, detailsDialog
  },
  data() {
    return {
      isWide: true,
      query: {
        total: 0,
        page: 1,
        limit: 20,
        dataType: null,
        packetStatus: null,
        outletId: null
      },
      dataTypes: [],
      packetStatuList: [],
      tableData: [],
      tableLoading: false,
      dialogVisible_d: false,
      obj: {},
      outletNames: []
    }
  },
  created() {
    const date = new Date()
    this.$set(this.query, 'endTime', getDatefmt(date, 'yyyy-MM-dd') + ' 23:59:59')
    this.$set(this.query, 'startTime', getDatefmt(new Date(new Date().setDate(date.getDate() - 1)), 'yyyy-MM-dd') + ' 00:00:00')

    this.handleSelect()
    this.dataTypes = getDict('e038')
    this.packetStatuList = getDict('e037')

    getList().then(res => {
      this.outletNames = res.data.data
    }).catch(() => {
    })
  },
  mounted() {
  },
  methods: {
    getDictLabel,
    parseTime,
    initData() {
      this.handleSelect()
    },
    // 查询
    handleSelect() {
      this.tableLoading = true
      const query = JSON.parse(JSON.stringify(this.query))
      getPage(query).then(res => {
        this.tableData = res.data.rows
        this.query.total = res.data.total
        this.$nextTick(() => {
          this.$refs.multipleTable.doLayout()
          // el-table加ref="multipleTable"
        })
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 导出
    handleExport() {
      const query = JSON.parse(JSON.stringify(this.query))
      exportPage(query).then(result => {
        downloadFileHasFile(result)
      }).catch(() => {
        console.log('此处错误')
      })
    },
    // 详情
    handle(row) {
      this.obj = row
      this.dialogVisible_d = true
    },
    closeDialog() {
      this.dialogVisible_d = false
    },
    // 点击树
    handleNodeClick(data) {
      if (data.level === 3) {
        this.showData = true
        this.query.outletId = data.id
        this.handleSelect()
      } else {
        this.$message.warning('请选择排口')
      }
    }
  }
}
</script>

<style lang='scss' scoped>
</style>
